package me.wangshier.leetcode.medium;

import java.util.HashMap;
import java.util.Map;

/**
 * 9. 超市里的货架调整
 *
 * @Author: tongqianwen
 * @Date: 2025/3/5
 */
public class ShelfAdjustment {
    public static int solution(int n, int m, String s, String c) {
        // 统计每个商品在货架上的出现次数
        Map<Character, Integer> countMap = new HashMap<>();
        for (char ch : s.toCharArray()) {
            countMap.put(ch, countMap.getOrDefault(ch, 0) + 1);
        }
        // 计算最多可以卖出多少件商品
        int sold = 0;
        for (int i = 0; i < c.length(); i++) {
            char ch = c.charAt(i);
            if (countMap.containsKey(ch) && countMap.get(ch) > 0) {
                countMap.put(ch, countMap.get(ch) - 1);
                sold++;
            }

        }
        return sold;
    }

    public static void main(String[] args) {
        System.out.println(solution(12, 8, "abbcbccabcaa", "bbbbbbbb") == 4);
        System.out.println(solution(3, 4, "abc", "abcd") == 3);
        System.out.println(solution(4, 2, "abbc", "bb") == 2);
        System.out.println(solution(5, 4, "bcdea", "abcd") == 4);
    }
}
